import javax.swing.*;
import java.awt.*;

public class Fireball
{
	private int xPos; // x position
	private int yPos; // y position
	private int direction; // The direction in which the fireball will be moving

	private boolean end = false; // this variable represents when a fireball hitts an occupied tiles (such as trees or walls)

	private Tile tile, newTile;
	private Tile[][] tiles; // array of all the tiles

	public Fireball (Tile tile, Tile[][] tiles, int direction)
	{
		this.tile = tile;	// all tiles
		this.tiles = tiles;	// starting tile
		this.xPos = tile.getPosX();	// original xPos
		this.yPos = tile.getPosY();	// original yPos
		this.direction = direction;	// direction of movement
	}

	public void move()
		// Very similar to the was the player moves. Depending on the direction, it sets specific 'newX' and 'newY'
		// values. These values are then used to reference a tile (newTile) next to the tile the fireball (depending on its direction)
		// If the new tile is not occupied, we set the new x and y positions of the fire ball to the x and y of the tile
		// If it is occupied, we set end to true and reverse direction
	{
		int newX = 0;
		int newY = 0;

		if (this.direction == 0)		// w + up
			newY = -1;
		else if (this.direction == 1)	// a + left
			newX = -1;
		else if (this.direction == 2)	// d + right
			newX = 1;
		else if (this.direction == 3)	// s + down
			newY = 1;

		newTile = tiles[(int)xPos / 16 + newX][(int)yPos / 16 + newY]; // new tile that's beside the existing tile that the fireball is currently on

		if(!newTile.getOccupied())
			tile = newTile;
		else
			end = true;

		xPos = tile.getPosX();
		yPos = tile.getPosY();
	}

	public void flipDirection() // changes direction (occurs when 'end' is true)
	{
		if(this.direction == 0)
			this.direction = 3;
		else if(this.direction == 1)
			this.direction = 2;
		else if(this.direction == 2)
			this.direction = 1;
		else if(this.direction == 3)
			this.direction = 0;

		this.end = !this.end; // usually returns end to false
	}

	// getters and setters
	public boolean getEnd()
	{
		return this.end;
	}

	public int getXPos()
	{
		return this.xPos;
	}

	public int getYPos()
	{
		return this.yPos;
	}
}